package fitnesshub.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import fitnesshub.mapper.CourseMapper;
import fitnesshub.pojo.dto.AddCourseDTO;
import fitnesshub.pojo.dto.CoursePageQueryDTO;
import fitnesshub.pojo.entity.Course;
import fitnesshub.result.PageResult;
import fitnesshub.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 课程分页
     * @param coursePageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CoursePageQueryDTO coursePageQueryDTO) {
        PageHelper.startPage(coursePageQueryDTO.getPage(), coursePageQueryDTO.getPageSize());
        Page<Course> page = courseMapper.pageQuery(coursePageQueryDTO);
        long total = page.getTotal();
        List<Course> records = page.getResult();
        return new PageResult(total, records);
    }

    /**
     * 根据id查询课程
     * @param courseId
     * @return
     */
    @Override
    public Course getById(Integer courseId) {
        return courseMapper.getById(courseId);
    }

    /**
     * 新增课程
     * @param addCourseDTO
     * @return
     */
    @Override
    public void addACoach(AddCourseDTO addCourseDTO) {
        Course course = new Course();

        BeanUtils.copyProperties(addCourseDTO, course);
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());

        courseMapper.insert(course);
    }

    /**
     * 编辑课程
     * @param course
     * @return
     */
    @Override
    public void editCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    /**
     * 根据id删除课程
     * @param courseId
     * @return
     */
    @Override
    public void deleteById(Integer courseId) {
        courseMapper.deleteById(courseId);
    }
}
